<template>
  <editor v-model="content" tag-name="div" :init="init" :style="{ zoom }" v-if="showEdit" />
</template>
<script setup>
import {
  ref,
  reactive,
  toRefs,
  watch,
  onMounted,
  getCurrentInstance
} from 'vue'

// tinymce插件可按自己的需要进行导入;更多插件参考：https://www.tiny.cloud/docs/plugins/
import Editor from "@tinymce/tinymce-vue";
import tinymce from "tinymce/tinymce";
import "tinymce/themes/silver/theme"; // 引用主题文件
import "tinymce/icons/default"; // 引用图标文件
import 'tinymce/models/dom'
import "tinymce/plugins/advlist"
import "tinymce/plugins/anchor"
import "tinymce/plugins/autolink"
import "tinymce/plugins/autoresize"
import "tinymce/plugins/autosave"
import "tinymce/plugins/charmap" // 特殊字符
import "tinymce/plugins/code" // 查看源码
import "tinymce/plugins/codesample" // 插入代码
import "tinymce/plugins/directionality"
import "tinymce/plugins/emoticons"
import "tinymce/plugins/fullscreen" //全屏
import "tinymce/plugins/help"
import "tinymce/plugins/image" // 插入上传图片插件
import "tinymce/plugins/importcss" //图片工具
import "tinymce/plugins/insertdatetime" //时间插入
import "tinymce/plugins/link"
import "tinymce/plugins/lists" // 列表插件
import "tinymce/plugins/media" // 插入视频插件
import "tinymce/plugins/nonbreaking"
import "tinymce/plugins/pagebreak" //分页
import "tinymce/plugins/preview" // 预览
import "tinymce/plugins/quickbars"
import "tinymce/plugins/save" // 保存
import "tinymce/plugins/searchreplace" //查询替换
import "tinymce/plugins/table" // 插入表格插件
import "tinymce/plugins/visualblocks"
import "tinymce/plugins/visualchars"
import "tinymce/plugins/wordcount" // 字数统计插件
// import "tinymce/plugins/print" // 打印

const props = defineProps({
  modelValue: String, // v-model双向绑定
  height: {
    type: Number,
    default: 720
  }
})

const emit = defineEmits(["update:modelValue"]) // v-model双向绑定

const showEdit = ref(false)  // 显示编辑框
const zoom = ref(1) // css缩放比例
const init = ref({}) // 配置对象 
const content = ref(props.modelValue) // 编辑器内容

watch(props, (newVal) => content.value = newVal.modelValue) // 监听v-model,更新编辑器内容
watch(content, (newVal) => { // 监听编辑器内容,更新v-model
  console.log(newVal)
  emit("update:modelValue", newVal)
})



// 初始化编辑器
const editInit = (height) => {
  showEdit.value = true

  // 配置编辑器初始化参数
  init.value = {
    language_url: '/node_modules/tinymce/langs/zh-Hans.js', // 修改为正确的语言包路径
    language: "zh-Hans",
    skin_url: '/node_modules/tinymce/skins/ui/oxide', // 修改为正确的皮肤路径
    content_css: "/node_modules/tinymce/skins/content/default/content.min.css", // 修改为正确的内容样式路径
    menubar: true, // 隐藏菜单栏
    autoresize_bottom_margin: 50,
    max_height: height,
    min_height: height,
    // height: props.height,
    toolbar_mode: "none",
    plugins:
      'wordcount visualchars visualblocks searchreplace save quickbars preview pagebreak nonbreaking media insertdatetime importcss image help fullscreen directionality codesample code charmap link code table lists advlist anchor autolink autoresize autosave',
    toolbar:
      "formats undo redo fontsize fontfamily ltr rtl searchreplace media | outdent indent aligncenter alignleft alignright alignjustify lineheight underline quicklink h2 h3 blockquote numlist bullist table removeformat forecolor backcolor bold italic strikethrough hr link preview fullscreen help ",
    content_style: "p {margin: 5px 0; font-size: 14px}",
    fontsize_formats: "12px 14px 16px 18px 24px 36px 48px 56px 72px",
    font_family_formats: "微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方= PingFang SC, Microsoft YaHei, sans- serif; 宋体 = simsun, serif; 仿宋体 = FangSong, serif; 黑体 = SimHei, sans - serif; Arial = arial, helvetica, sans - serif;Arial Black = arial black, avant garde;Book Antiqua = book antiqua, palatino; ",
    branding: false,
    elementpath: false,
    // resize: false, // 禁止改变大小
    statusbar: false, // 隐藏底部状态栏 
    promotion: false, //去掉upgrade
    init_instance_callback: function (editor) {
      console.log(111, editor, 'init_instance_callback');
    },

  }

  // console.log(tinymce);
  tinymce.init

}

onMounted(() => {
  // 窗口缩放
  zoom.value = 1 / document.documentElement.style.zoom
  window.addEventListener('resize', () => {
    zoom.value = 1 / document.documentElement.style.zoom
  })
  console.log(props.height, 'props.height');
})

defineExpose({ editInit }) // 暴露给父组件
</script>
<style scoped>
.tox-tinymce-aux {
  z-index: 9999 !important;
}
</style>